from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from django.views import View
from rest_framework.response import Response
from rest_framework.views import APIView
# Create your views here.
from functools import wraps
from django.utils.decorators import method_decorator
from .serve import *

#redis
import redis

conn = redis.Redis('localhost')

#获取验证码图片
from captcha.captcha.captcha import captcha


class GoodsAPI(APIView):
    def get(self,request):
        goods = Goods.objects.all()
        goods_ser = GoodsSer(goods,many=True)
        
        return Response({
            'data':goods_ser.data
        })




#获取作者信息
class GetAuthor(APIView):
    def get(self,request):
        author = Author.objects.all()
        author_ser = AuthorSer(author,many=True)

        return Response({
            'data':author_ser.data
        })



#添加图书
class AddBook(APIView):
    def get(self,request):
        name = request.GET.get('name')
        price = request.GET.get('price')
        authorid = request.GET.get('id')

        # print(name,price,authorid)
        author = Author.objects.get(id = int(authorid))
        Book.objects.create(name=name,price=price,author=author)



        return Response({
            'data':'添加成功'
        })



#获取图书信息
class GetBook(APIView):
    def get(self,request):
        book = Book.objects.all()
        book_ser = BookSer(book,many=True)

        return Response({
            'data':book_ser.data
        })

# 删除图书
class DelBook(APIView):
    def get(self,request):
        id = request.GET.get('id')
        book = Book.objects.get(id=int(id))
        book.delete()
        
        return Response({
            'data':'删除成功'
        })



#注册
class Register(APIView):
    def post(self,request):
        #获取用户名
        username = request.data.get('username')
        #获取用户输入的验证码
        usercode = request.data.get('code')
        #查询存好的正确的验证码
        code = conn.get(username)
        code = str(code,encoding='utf-8')
        print (code.lower())
        print(usercode)
        if code:
            #判断两个验证码是否相同
            if str(code).lower() == usercode.lower():
                
                user = UserSer(data=request.data)
                if user.is_valid():
                    user.save()

                    return Response({
                        'code':200
                    })
            else:
              return Response({
                'code':301
            })
        else:
            return Response({
                'code':300
            })


#登陆
class Login(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        print(username,password)
        user = User.objects.filter(username=username,password=password)
        if user:
            return Response({
                'code':200
            })
        return Response({
            'code':300
            })   




#获取验证码图片
class Image(View):
    def get(self,request):

        name, text, image = captcha.generate_captcha()
        
        #获取用户输入的用户名
        username = request.GET.get('username')
        print('用户名为：',username)
        #将验证码存入redis
        conn.set(username,text,60) #为防止多人注册导致验证码出错，将key存为用户名，value存为验证码

        response = HttpResponse(image)
        response['Content-Type'] = 'image/jpg'
        return response



